package com.hafu.view.RuKu;
import com.hafu.model.RuKuMain;
import com.hafu.service.RuKuService;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
public class QueryRuKuFrm extends JFrame {
    private RuKuService ruKuService=new RuKuService();
    public QueryRuKuFrm() {
        initComponents();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        ArrayList<RuKuMain> ruKuList = ruKuService.getRuKuList(new RuKuMain());
        for (RuKuMain ruKu : ruKuList) {
            Object[] rowData = {ruKu.getId(),ruKu.getRuKuName(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRuKuDate()};
            defaultTableModel.addRow(rowData);
        }
    }
    private void button1(ActionEvent e) {
        String text = textField1.getText();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRuKuName(text);
        ArrayList<RuKuMain> ruKuList = ruKuService.getRuKuList(ruKuMain);
        for (RuKuMain ruKu : ruKuList) {
            Object[] rowData = {ruKu.getId(),ruKu.getRuKuName(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRuKuDate()};
            defaultTableModel.addRow(rowData);
        }
    }
    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        textField2.setText(name);
    }
    private void button2(ActionEvent e) {
        String name = textField2.getText();
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setId(chooseId);
        ruKuMain.setRuKuName(name);
        Boolean aBoolean = ruKuService.updateRuKu(ruKuMain);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }
    private void initComponents() {
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        button2 = new JButton();
        setTitle("\u67e5\u8be2\u5165\u5e93\u5355");
        Container contentPane = getContentPane();
        label1.setText("\u5165\u5e93\u5355\u540d\u5b57");
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                    },
                    new String[] {
                            "\u7f16\u53f7", "\u540d\u5b57", "\u603b\u4ef7", "\u4f9b\u5e94\u5546\u540d\u5b57", "\u4e0b\u5355\u65f6\u95f4"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        label2.setText("\u5165\u5e93\u5355\u540d\u5b57");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(42, 42, 42)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 730, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label1)
                                                .addGap(60, 60, 60)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
                                                .addGap(133, 133, 133)
                                                .addComponent(button1))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
                                                .addGap(60, 60, 60)
                                                .addComponent(button2)))
                                .addContainerGap(151, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button1))
                                .addGap(34, 34, 34)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button2))
                                .addContainerGap(44, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button2;
}
